16

我有一张 40 个方格的桌子,每个方格都有一个 ID。当我从另一个活动传递捆绑包时,我需要从该捆绑包中提取注释、颜色和 ID。然后应用程序将更改/添加文本并更改提取的 ID 指定的正方形的背景。每个方格都有 int 格式的 ID。从其他活动传递的每个 ID 都是字符串格式。我不知道如何使它成为 findViewById(R.id.passed_id),以及如何让两种不同的格式一起工作。我试图更改每个正方形的 ID,但 Eclipse 说 ID 必须有一个字母和一个数字。我迷路了......这是代码:

package com.tt;

 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Intent;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity
 {
String gotNotes;
String n;
String gotDOW;
String gotID;
public String Id;
String gotHour;
TextView notes;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();
    Button settings = (Button)findViewById(R.id.settings);

    Bundle gotPackage = getIntent().getExtras();
    if (gotPackage != null){
    gotNotes = gotPackage.getString("AddedNote");
   // if (gotNotes.equals(" ")){n = "Empty";}else n = gotNotes;
    //gotDOW = gotPackage.getString("Day");
    //gotHour = gotPackage.getInt("Hour");
    gotID = gotPackage.getString("ID");

    Id = gotID;
    notes.setText(gotNotes + (" \n") + gotID);

    }
    else{}



  settings.setOnClickListener(new OnClickListener()
    {



          public void onClick(View v)
         {
              Intent i = new Intent(v.getContext(),Settings.class);
              startActivityForResult(i,0);
         }

     });

   }

  private void initialize() 
       {
    // TODO Auto-generated method stub


    notes = (TextView)findViewById(R.id.);
    notes.setGravity(Gravity.CENTER);
    notes.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
}



    @Override
            public boolean onCreateOptionsMenu(Menu menu) 
             {
               getMenuInflater().inflate(R.menu.activity_main, menu);
               return true;

             }

      }

更新

Ok.. here's the log:

12-27 16:18:55.661: D/AndroidRuntime(12497): Shutting down VM
12-27 16:18:55.661: W/dalvikvm(12497): threadid=1: thread exiting with uncaught exception (group=0x40abf228)
12-27 16:18:55.671: E/AndroidRuntime(12497): FATAL EXCEPTION: main
12-27 16:18:55.671: E/AndroidRuntime(12497): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tt/com.tt.MainActivity}: java.lang.NullPointerException
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.os.Looper.loop(Looper.java:154)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.main(ActivityThread.java:4944)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at java.lang.reflect.Method.invokeNative(Native Method)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at java.lang.reflect.Method.invoke(Method.java:511)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at dalvik.system.NativeStart.main(Native Method)
12-27 16:18:55.671: E/AndroidRuntime(12497): Caused by: java.lang.NullPointerException
12-27 16:18:55.671: E/AndroidRuntime(12497):    at com.tt.MainActivity.initialize(MainActivity.java:69)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at com.tt.MainActivity.onCreate(MainActivity.java:30)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.Activity.performCreate(Activity.java:4531)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
12-27 16:18:55.671: E/AndroidRuntime(12497):    ... 11 more
4

3 回答 3

37

You can get an identifier from a string by using:

notes = (TextView)findViewById(getResources().getIdentifier(VIEW_NAME, "id", getPackageName()));

Where VIEW_NAME is whatever identifier string you're generating. After that, you can set the text of it like you currently are. This also works if you need to get strings and drawables as well, just change id to the appropriate type.

于 2012-12-27T16:49:34.227 回答
0

Assuming you're passing in the actual string "R.id.passed_id", I would recommend passing in the integer value instead. Thus, simply pass in R.id.passed_id.

All values in the R file of an application are simply integers that are mapped to resource locations. The findById() methods are only looking for integers, so if you start the Activity and pass in the integer value of the resource you want, it will find it.

Note: This will only work if the resources are within the same application. R files remain constant for an application, but they can change from one app to another even if they have the same name. If, however, the two activities fall under the same application package, it will work.

于 2012-12-27T15:18:56.687 回答
-2

Man I'm not understating what exactly you want to do. Id is the id off an android widget then on function initialize() you should cast string to int this way: Integer.ValueOf(Id) Hope this is what you want.

于 2012-12-27T15:20:37.067 回答