アンドロイドのプログラミングでim begginerと私は多くのビューとinflatting作業を理解していません。 これは私がやりたいことです:私は別のレイアウトを含む1つのメインレイアウトを持っていると思います。この「別の」レイアウトは、Javaとメインアクティビティをプログラムレイアウトで変更してメインレイアウトに組み込むことを望んでいます。Android - Javaのレイアウト内にレイアウトを挿入
は私がkalendar_view.xmlに
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
を含める線形レイアウトmain_content内部activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="blahblah.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
<!--here i want to include another layout-->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_content"
android:orientation="horizontal"></LinearLayout>
<!--
<include layout="@layout/content_main" />
-->
</android.support.design.widget.CoordinatorLayout>
持っており、これは私のMainActivity.java
public class MainActivity extends AppCompatActivity {
TextView tv;
LinearLayout mainContent;
KalendarView kv;
Context context;
LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater=getLayoutInflater();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
mainContent=(LinearLayout) findViewById(R.id.main_content);
kv = new KalendarView(context, inflater);//this view i want to include in main layout
mainContent.addView(kv);//trying to include
setSupportActionBar(toolbar);
tv= (TextView) findViewById(R.id.textView11);
}
(...)
とKalendarViewです.java
public class KalendarView extends LinearLayout {
// LayoutInflater inflater;
public KalendarView(Context context, LayoutInflater inflater){
super(context);
// inflater = (LayoutInflater)context.getSystemService
// (Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.kalendar_view, null);//trying to inflate
}
//(...) some methods to programically change layout
}
このアプリはクラッシュして、なぜ私は知らないのですか? 私は、それらのうちの1つをプログラムで表示できるように、複数の異なるインスタンスのKalendarViewクラスを用意することを目標としています。
私のコードに何が間違っているのか、別のレイアウトを正しく組み込む方法を教えてもらえますか?
投稿するにはログインしてください。 –
マイクの答えの後、アプリケーションはもはやクラッシュしませんが、それはまた、activity_main.xml内にkalendar_view.xmlを表示しません – cartmaneric
あなたのログ猫を共有することはできますか? –