感谢任何帮助。我还在学习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 Second Fragment,它从列表中提取数据并应该发送到listofclasses。
<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>
答案 0 :(得分:0)
我认为您在没有实例化的情况下使用listofclasses
。因此得到NullPointerException
。
BTW
您是否正在调用refreshview()
方法来填充矢量?
答案 1 :(得分:0)
这是因为传递给数组适配器的listOfClasses为null。 在你把它传递到那里之前你必须填写这个清单...尝试这个解决方案......
只是你NullPointerException
Caused by: java.lang.NullPointerException
at com.drexel.drexelgpacalc.MainActivity.setupeverything(MainActivity.java:245)
at com.drexel.drexelgpacalc.MainActivity.onCreate(MainActivity.java:49)
只需点击两次
at com.drexel.drexelgpacalc.MainActivity.setupeverything(MainActivity.java:245)
和eclipse会告诉你麻烦在哪里。 你必须认识到什么变量有空值.. 认为这是list_of_classes。
LogCat对开发人员来说是一个强大的收费;)
答案 2 :(得分:0)
正如评论中指出的那样。
在onCreate
setContentView(R.layout.main)
main.xml
但R.layout.activity_main
没有在其中定义列表视图且ID为“listview_classes”。
您有一个视图寻呼机,但我没有看到您使用layoutinflater
的任何地方。您需要使用R.layout.activity_main
来设置setContentView(R.layout.activity_main)
并在视图寻呼机上设置它,或者只是放弃查看寻呼机的整个想法。
看起来只是{{1}}会更简单,并删除有关视图寻呼机的代码,除非你有其他理由想要它。