どんな助けでも大歓迎です。私はまだ Android を学習中で、多くの質問に目を通しましたが、解決策が見つからないようです。繰り返しますが、助けてくれてありがとう!
setupeverything メソッドの FragmentActivity で ArrayAdapter を適切にセットアップできないというエラーが発生します。年と用語を提供するスピナーがありますが、list.setAdapter がエラーをスローする理由がわかりません... これまでのコードは次のとおりです...
public class MainActivity extends FragmentActivity {
private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;
variables here...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpView();
setupeverything(); //LINE 49
}
private void setUpView(){
_mViewPager= (ViewPager) findViewById(R.id.viewPager);
_adapter = new ViewPagerAdapter(getApplicationContext(), getSupportFragmentManager());
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
}
public static void refreshview()
{
int year = spinner_year.getSelectedItemPosition();
int term = spinner_term.getSelectedItemPosition();
listofclasses.clear();
for(int i=0;i<terms[year][term].getSize();i++)
{
listofclasses.add(terms[year][term].getCourse(i).getname() + " " + terms[year][term].getCourse(i).getcredits()+ " " + terms[year][term].getCourse(i).getgrade());
}
adapterthing.notifyDataSetChanged();
setcreditandgpa();
}
public void buttonaddclass(View view)
{
int year = spinner_year.getSelectedItemPosition();
int term = spinner_term.getSelectedItemPosition();
terms[year][term].addClass("test"+count, "B+", 4);
refreshview();
//Intent openaddclass = new Intent("AddClassActivity");
//startActivity(openaddclass); //Removed because changed to fragments
}
public void setupeverything()
{
ListView list = (ListView) findViewById(R.id.listview_classes);
adapterthing = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, listofclasses);
list.setAdapter(adapterthing); //LINE 245
list.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
deleteclass(position);
return true;
}});
array_year=new String[5];
array_year[0]="1";
array_year[1]="2";
array_year[2]="3";
array_year[3]="4";
array_year[4]="5";
spinner_year = (Spinner) findViewById(R.id.spinner_year);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array_year);
spinner_year.setAdapter(adapter);
array_term=new String[4];
array_term[0]="F";
array_term[1]="W";
array_term[2]="S";
array_term[3]="S";
spinner_term = (Spinner) findViewById(R.id.spinner_term);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array_term);
spinner_term.setAdapter(adapter1);
for (int i = 0; i<5; i++)
{
for(int j = 0; j<4;j++)
{
terms[i][j] = new ClassList();
}
}
spinner_year.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
refreshview();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner_term.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
refreshview();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
edittotalcredit = (EditText) findViewById(R.id.edittext_credits);
edittermcredit = (EditText) findViewById(R.id.edittext_credits_term);
edittotalgpa = (EditText) findViewById(R.id.edittext_gpa);
edittermgpa = (EditText) findViewById(R.id.edittext_gpa_term);
}
LogCat:
05-29 00:40:31.415 2713-2713/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.drexel.drexelgpacalc/com.drexel.drexelgpacalc.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.drexel.drexelgpacalc.MainActivity.setupeverything(MainActivity.java:245)
at com.drexel.drexelgpacalc.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
XML レイアウト:
activity_main.xml は、addclasslayout.xml から選択したコース リストのデータを表示するために表示される最初のフラグメントです。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Spinner
android:id="@+id/spinner_year"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Spinner
android:id="@+id/spinner_term"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
<ListView
android:id="@+id/listview_classes"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginTop="15dp"
android:layout_weight="4" >
</ListView>
<Button
android:id="@+id/button_submit"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Add Class"
android:onClick="buttonaddclass" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/textview_credits_term"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Credits This Term:"
android:textSize="18sp" />
<EditText
android:id="@+id/edittext_credits_term"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:focusable="false"
android:hint=" "
android:longClickable="false" />
<TextView
android:id="@+id/textview_gpa_term"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="GPA This Term:"
android:paddingLeft="20dp"
android:textSize="18sp" />
<EditText
android:id="@+id/edittext_gpa_term"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:focusable="false"
android:hint=" "
android:longClickable="false" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/textview_credits"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Credits Overall:"
android:textSize="18sp" />
<EditText
android:id="@+id/edittext_credits"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:focusable="false"
android:hint=" "
android:longClickable="false" />
<TextView
android:id="@+id/textview_gpa"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="GPA Overall:"
android:paddingLeft="20dp"
android:textSize="18sp" />
<EditText
android:id="@+id/edittext_gpa"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:focusable="false"
android:hint=" "
android:longClickable="false" />
</LinearLayout>
</LinearLayout>
Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewPager" />
</LinearLayout>
AddClassLayout.xml リストからデータを取得し、listofclasses に送信することになっている 2 番目のフラグメント。
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonclick"
android:text="Add Class" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_weight="3.5"
android:layout_height="wrap_content"
android:hint="Start Entering Class Here"
android:ems="10"
android:singleLine="true"/>
<Spinner
android:id="@+id/expandableListView2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:hint="Grade"
android:entries="@array/gradeArray" />
</LinearLayout>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>