English 中文(简体)
Random string on Button press
原标题:Random string on button press

我试图从一个顿新闻时的阵列中随机抽取。

迄今为止,在申请启动时,它一直在随机地进行扼杀,但我无法在启动后就着手处理一个纽州的新闻。

EDIT: It now crashes on start up??

我的新法典:

private String[] myString;
private static final Random rgenerator = new Random();


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    TextView tv = (TextView) findViewById(R.id.joke);
  Resources res = getResources();

    myString = res.getStringArray(R.array.Jokes); 

    String q = myString[rgenerator.nextInt(myString.length)];    
    tv.setText(q);
    Button btn = (Button)findViewById(R.id.button);


 btn.setOnClickListener(new OnClickListener() { 
public void onClick(View v) {

    myString = res.getStringArray(R.array.Jokes); 

    String q = myString[rgenerator.nextInt(myString.length)];    
    tv.setText(q);
  }
});

}
}

广播公司说:

03-31 22:47:35.777: W/dalvikvm(1152): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-31 22:47:35.777: E/AndroidRuntime(1152): Uncaught handler: thread main exiting due to uncaught exception
03-31 22:47:35.903: E/AndroidRuntime(1152): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.jc.dkjokes/com.jc.dkjokes.DanskeJokesMain}: java.lang.NullPointerException
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.os.Looper.loop(Looper.java:123)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at java.lang.reflect.Method.invoke(Method.java:521)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at dalvik.system.NativeStart.main(Native Method)
03-31 22:47:35.903: E/AndroidRuntime(1152): Caused by: java.lang.NullPointerException
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.Activity.findViewById(Activity.java:1612)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at com.jc.dkjokes.DanskeJokesMain.<init>(DanskeJokesMain.java:17)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at java.lang.Class.newInstanceImpl(Native Method)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at java.lang.Class.newInstance(Class.java:1479)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
03-31 22:47:35.903: E/AndroidRuntime(1152):     ... 11 more
03-31 22:47:35.963: I/dalvikvm(1152): threadid=7: reacting to signal 3
03-31 22:47:36.148: I/dalvikvm(1152): Wrote stack trace to  /data/anr/traces.txt 
  • SnoX
最佳回答

为了实现你需要在主编xml档案中宣布一个纽州,你必须首先在你的活动中这样做。 之后,你不得不在该顿点上点击事件听众。

<Button
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:id="@+id/buttonClick"
    />

<><><>>>>

TextView tv = (TextView) findViewById(R.id.joke);
Button btn = (Button)findViewbyId(R.id.buttonClick);
    btn.setonclickListener(new onClickListner(){
          public void onclick()
          {
               myString =  getResources().getStringArray(R.array.Jokes); 
               String q = myString[rgenerator.nextInt(myString.length)];
               tv.setText(q);
          }
    });
问题回答

您必须在ClickListener()上下定义,以在纽特州点击点上铺设,如上,

    Button btn = (Button)findViewbyId(R.id.yourbuttonid);
    btn.setonclickListener(new onClickListner(){
          public void onclick()
          {
               //write your oncreate code here again
          }
    });

页: 1 具体如下:

Button button = findViewById(R.id.button);
TextView tv = findViewById(R.id.joke);

button.setOnClickListener(new OnClickListener()  
    public void onClick(View view) {
        String[] myString = getResources().getStringArray(R.array.Jokes); 
        tv.setText(myString[rgenerator.nextInt(myString.length)]);
    }
});




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签