无法将String设置为我想要的任何内容

时间:2013-06-09 13:00:28

标签: java android

这是整个Java类。问题是:answerList.add(position,item);被激活了。如果我将项目更改为“ - ”然后它可以工作但如果我发送任何其他字符串它不起作用。那真是怪了。但我希望能够添加项目。我该怎么办。

现在应用程序崩溃时,该功能应该发生添加。

公共类SinglePlayerQuickGame扩展了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;
    }

  }

}

错误消息:

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

2 个答案:

答案 0 :(得分:0)

您无法更改它,因为它声明的final是一个声明为final的引用变量,并且分配给一个引用变量无法分配给另一个对象的对象。所以变量s不能引用任何其他东西,除了它已经存在的东西。

答案 1 :(得分:0)

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

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

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