0

This is the whole Java class. The problem is when: answerList.add(position, item); is activated. if I change item to "-" then it works but if I send any other string it doesn't work. That's weird. But I want to be able to add item. What should I do.

Right now the app crashes when that function the add is supposed to happen.

public class SinglePlayerQuickGame extends Activity {

databaseTest testDatabase=new databaseTest();
boolean alternativeIsChosen; //är true om ett alternativ har valts
int chosenAlternative;       //om ett alternativ har valts representer
 String alternativeText;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
@SuppressLint("NewApi") 
@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_player_quick_game);
    alternativeIsChosen=false;
    chosenAlternative=-1;
    alternativeText="";

    final ListView AnswerListview = (ListView) findViewById(R.id.chosenAlternatives);
    final ListView alternativeListview= (ListView) findViewById(R.id.alternatives);


    final ArrayList<String> answerList = new ArrayList<String>();
    final ArrayList<String> alternativeList=new ArrayList<String>();


    for(int i=0; i<testDatabase.alternatives.getSize();i++){
        alternativeList.add(testDatabase.alternatives.getNameOnAPosition(i));
    }

    answerList.add("-");
    for(int i=0; i<testDatabase.chosenAlternatives.getSize();i++){
        answerList.add(testDatabase.chosenAlternatives.getNameOnAPosition(i));
        answerList.add("-");

    final StableArrayAdapter answerAdapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, answerList);
    AnswerListview.setAdapter(answerAdapter);

    final StableArrayAdapter alternativeAdapter= new StableArrayAdapter(this,android.R.layout.simple_list_item_1,alternativeList );
    alternativeListview.setAdapter(alternativeAdapter); 

    AnswerListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    @SuppressLint("NewApi")

    public void onItemClick(AdapterView<?> parent, final View view,
          final int position, long id) {

          final String item=(String) alternativeListview.getItemAtPosition(0);

        view.animate().setDuration(0).alpha(0)
            .withEndAction(new Runnable() {
              @SuppressWarnings("null")
            @Override
              public void run() {

                  if(alternativeIsChosen){


                     answerList.add(position, item);
                     answerAdapter.notifyDataSetChanged();
                     alternativeList.remove(chosenAlternative);
                     alternativeAdapter.notifyDataSetChanged();
                     alternativeIsChosen=false;
                  }



                view.setAlpha(1);
              }
            });
      }



    });

    alternativeListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

          @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
        @SuppressLint("NewApi")


        public void onItemClick(AdapterView<?> parent, final View view,
              final int position, long id) {


           final String answerItem = (String) parent.getItemAtPosition(position);

            view.animate().setDuration(0).alpha(0)
                .withEndAction(new Runnable() {
                  @Override
                  public void run() {

                    alternativeIsChosen=true;
                    chosenAlternative=position;

                     answerAdapter.notifyDataSetChanged();
                     alternativeAdapter.notifyDataSetChanged();
                    view.setAlpha(1);
                  }
                });
          }



        });
  }

public final String getAlternativeText(){
    return alternativeText;
}

  private class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
        List<String> objects) {
      super(context, textViewResourceId, objects);
      for (int i = 0; i < objects.size(); ++i) {
        mIdMap.put(objects.get(i), i);
      }
    }

    @Override
    public long getItemId(int position) {
      String item = getItem(position);
      return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
      return true;
    }

  }

}

The error messages:

06-10 10:43:36.009: D/AndroidRuntime(618): Shutting down VM
06-10 10:43:36.009: W/dalvikvm(618): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
06-10 10:43:36.159: E/AndroidRuntime(618): FATAL EXCEPTION: main
06-10 10:43:36.159: E/AndroidRuntime(618): java.lang.NullPointerException
06-10 10:43:36.159: E/AndroidRuntime(618):  at com.example.rankingquiz2.SinglePlayerQuickGame$StableArrayAdapter.getItemId(SinglePlayerQuickGame.java:198)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.AbsListView.obtainView(AbsListView.java:2292)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.ListView.makeAndAddView(ListView.java:1769)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.ListView.fillDown(ListView.java:672)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.ListView.fillSpecific(ListView.java:1330)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.ListView.layoutChildren(ListView.java:1600)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.AbsListView.onLayout(AbsListView.java:2106)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.View.layout(View.java:13754)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.ViewGroup.layout(ViewGroup.java:4364)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.View.layout(View.java:13754)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.ViewGroup.layout(ViewGroup.java:4364)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1638)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1422)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.View.layout(View.java:13754)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.ViewGroup.layout(ViewGroup.java:4364)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.View.layout(View.java:13754)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.ViewGroup.layout(ViewGroup.java:4364)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.View.layout(View.java:13754)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.ViewGroup.layout(ViewGroup.java:4364)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.View.layout(View.java:13754)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.ViewGroup.layout(ViewGroup.java:4364)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.View.layout(View.java:13754)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.ViewGroup.layout(ViewGroup.java:4364)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1868)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1689)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.Choreographer.doFrame(Choreographer.java:525)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.os.Handler.handleCallback(Handler.java:615)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.os.Looper.loop(Looper.java:137)
06-10 10:43:36.159: E/AndroidRuntime(618):  at android.app.ActivityThread.main(ActivityThread.java:4745)
06-10 10:43:36.159: E/AndroidRuntime(618):  at java.lang.reflect.Method.invokeNative(Native Method)
06-10 10:43:36.159: E/AndroidRuntime(618):  at java.lang.reflect.Method.invoke(Method.java:511)
06-10 10:43:36.159: E/AndroidRuntime(618):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-10 10:43:36.159: E/AndroidRuntime(618):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-10 10:43:36.159: E/AndroidRuntime(618):  at dalvik.system.NativeStart.main(Native Method)
06-10 10:43:36.269: D/dalvikvm(618): GC_CONCURRENT freed 202K, 4% free 6282K/6535K, paused 74ms+9ms, total 200ms
06-10 10:43:36.279: D/dalvikvm(618): WAIT_FOR_CONCURRENT_GC blocked 49ms
4

2 回答 2

0

目前尚不清楚问题究竟是什么。

但是,查看代码极不可能是“将字符串设置为变量”的结果。

这里还有其他事情……你没有提供足够的信息让我们弄清楚。您需要提供的第一件事是导致崩溃的异常的堆栈跟踪。

于 2013-06-09T14:35:18.413 回答
0

您不能更改它,因为它声明为 final 一个引用变量声明为 final 并分配给一个对象,该引用变量不能分配给另一个对象。所以变量 s 不能引用其他任何东西,只能引用它已经拥有的东西。

于 2013-06-09T13:27:01.363 回答